package cn.neusoft.controller;

import cn.neusoft.domain.Operator;
import cn.neusoft.exception.AppException;
import cn.neusoft.exception.AppExceptionCodeMsg;
import cn.neusoft.query.OperatorQuery;
import cn.neusoft.service.OperatorService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/operator", produces = "application/json;charset=utf-8")
@Api(value = "员工的API", description = "员工相关的CRUD功能")
public class OperatorController {

    @Autowired
    OperatorService operatorService;

//    @Autowired
//    private RedisTemplate<String, String> redisTemplate;

    @PostMapping
    @ApiOperation(value = "新增员工")
    public ResultBean add(@RequestBody Operator operator) {

        if (operator == null
                || StringUtils.isEmpty(operator.getOperator_name())
                || StringUtils.isEmpty(operator.getOperator_account())
                || StringUtils.isEmpty(operator.getOperator_password())
                || StringUtils.isEmpty(operator.getTel())
                || operator.getDept_id() == null
                || operator.getRole_id() == null
                || StringUtils.isEmpty(operator.getStatus())
                || StringUtils.isEmpty(operator.getSex())
        ) {

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        operatorService.add(operator);

        return ResultBean.success();
    }

    @PutMapping
    @ApiOperation(value = "根据id修改员工信息")
    public ResultBean update(@RequestBody Operator operator) {

        if (operator == null
                || StringUtils.isEmpty(operator.getOperator_id())
                || StringUtils.isEmpty(operator.getOperator_name())
                || StringUtils.isEmpty(operator.getOperator_account())
                || StringUtils.isEmpty(operator.getOperator_password())
                || StringUtils.isEmpty(operator.getTel())
                || operator.getDept_id() == null
                || operator.getRole_id() == null
                || StringUtils.isEmpty(operator.getStatus())
                || StringUtils.isEmpty(operator.getSex())
        ) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        operatorService.update(operator);

        return ResultBean.success();
    }

    @PutMapping("/updateStatus")
    @ApiOperation(value = "根据id修改员工状态")
    public ResultBean updateStatus(@RequestBody Operator operator) {

        if (operator == null
                || StringUtils.isEmpty(operator.getOperator_id())
                || StringUtils.isEmpty(operator.getStatus())
        ) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        operatorService.updateStatus(operator);

        return ResultBean.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除员工")
    public ResultBean deleteById(@PathVariable("id") String operator_id) {

        if (StringUtils.isEmpty(operator_id)){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        operatorService.deleteById(operator_id);

        return ResultBean.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询员工")
    public ResultBean findById(@PathVariable("id") String operator_id) {

        if (StringUtils.isEmpty(operator_id)){
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        Operator operator = operatorService.findById(operator_id);

        return ResultBean.success(operator);
    }

    @GetMapping("/all")
    @ApiOperation(value = "查询所有员工")
    public ResultBean findAll() {
        List<Operator> operatorList = operatorService.findAll();

        return ResultBean.success(operatorList);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页模糊查询员工信息")
    public ResultBean findByPage(@RequestBody OperatorQuery operatorQuery){
        if (operatorQuery == null
                || StringUtils.isEmpty(operatorQuery.getPageNow())
                || StringUtils.isEmpty(operatorQuery.getPageSize())
        ){

            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }

        PageBean<Operator> pageBean = operatorService.findByPage(operatorQuery);

        return ResultBean.success(pageBean);
    }
//    @PostMapping("/login")
//    @ApiOperation("通过账号密码登录")
//    public ResultBean loginByPwd(@RequestBody Map<String,String> map){
//        String account = map.get("no");
//        String password =  map.get("password");
//        if(StringUtils.isEmpty(account)||StringUtils.isEmpty(password)){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
//        }
//        Map<String, Object> mmp = operatorService.loginByPwd(account,password);
//
//        return ResultBean.success(mmp);
//    }
//
//    @PostMapping("/sendSms")
//    @ApiOperation("发送短信验证码")
//    public ResultBean sendSms(@RequestBody Map<String,String> map){
//        if (map == null){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
//        }
//
//        String tel = map.get("tel");
//
//        if (StringUtils.isEmpty(tel)){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
//        }
//
//        Operator operator = operatorService.findByTel(tel);
//
//        if (operator == null){
//            throw new AppException(AppExceptionCodeMsg.OPERATOR_TEL_NOT_EXITS);
//        }
//
//        String Vcode = RandomUtil.getSixBitRandom();
//        String[] params = {Vcode, "5"};
//        operatorService.send(tel, params);
//
//        //存入redis，设置验证码5分钟失效
//        redisTemplate.opsForValue().set(tel, Vcode,5, TimeUnit.MINUTES);
//        return ResultBean.success();
//    }
//
//
//    @PostMapping("/loginByTel")
//    @ApiOperation("通过手机验证码登录")
//    public ResultBean loginByTel(@RequestBody Map<String,String> map){
//        if (map == null){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
//        }
//
//        String tel = map.get("tel");
//        String code = map.get("code");
//
//        if (StringUtils.isEmpty(tel) || StringUtils.isEmpty(code)){
//            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
//        }
//
//        Map<String, Object> map1 = operatorService.loginByTel(tel, code);
//
//        return ResultBean.success(map1);
//    }


}
